package clientGUI.adminGUI;

import javax.swing.*;
import javax.swing.border.TitledBorder;

import clientGUI.PassBackToable;
import clientGUI.SelectCoursesGUI_Catalog;
import clientGUI.studentGUI.GUI_Function_Library;

import clientApp.Course;
import clientApp.VirtualAdvisor;

import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;

public class EditCourseGUI implements PassBackToable{
	private static VirtualAdvisor va;
	private JPanel editPrereqChosenPane;
	private LinkedList<JCheckBox> prereqList;
	private JFrame frame;
	/** DRIVER
	 * 
	 * @param args
	 */
	
	public static void main(String[] args)
	{
		EditCourseGUI ec = new EditCourseGUI("CMSC 201", new VirtualAdvisor());
	}
	
	public EditCourseGUI(VirtualAdvisor va)
	{
		this(new Course(), va);
	}
	public EditCourseGUI(String courseID, VirtualAdvisor va)
	{
		this(va.getCourseFromCatalog(courseID), va);
	}
	public EditCourseGUI(Course course, VirtualAdvisor va)
	{
		this.va = va;
		
		JPanel editCoursePane = editCoursePanel(course);
		frame = GUI_Function_Library.displayPane("Edit Course: " + course, editCoursePane, false);

	}
	
	public JPanel editCoursePanel(Course c)
	{
		JPanel editIDPane		= new JPanel(new GridLayout(1,2));
		JLabel editIDLabel		= new JLabel("Course ID: ");
		final JTextField editIDField	= new JTextField(c.getCourseCode());
		editIDPane.add(editIDLabel);
		editIDPane.add(editIDField);
		
		JPanel editTitlePane	= new JPanel(new GridLayout(1,2));
		JLabel editTitleLabel	= new JLabel("Course Title: ");
		final JTextField editTitleField = new JTextField(c.getCourseName());
		editTitlePane.add(editTitleLabel);
		editTitlePane.add(editTitleField);
		
		JPanel editCredNumPane	= new JPanel(new GridLayout(1,2));
		JLabel editCredNumLabel = new JLabel("# Credits: ");
		final JTextField editCredNumField = new JTextField(Double.toString(c.getNumCreds()));
		editCredNumPane.add(editCredNumLabel);
		editCredNumPane.add(editCredNumField);
		
		JPanel editPrereqPane	= new JPanel(new GridLayout(1,2));
		editPrereqPane.setBorder(new TitledBorder("Prerequisite"));
		//
		editPrereqChosenPane = new JPanel();
		//
		JButton editPrereqButton = new JButton("Add Prerequisites");
		editPrereqButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg)
			{
				try {
//					selectPrereqWorkaround();
					SelectCoursesGUI_Catalog selectCourses = new SelectCoursesGUI_Catalog(EditCourseGUI.this, "add_prereqs", false, va);
				} catch (InterruptedException e1) {
					GUI_Function_Library.exceptionHandler("EDITCOURSE:EDITPREREQ:", e1);
				}
			}
		});
		editPrereqPane.add(editPrereqChosenPane);
		editPrereqPane.add(editPrereqButton);
		
		JPanel editAvailPane	= new JPanel(new GridLayout(1,2));
		editAvailPane.setBorder(new TitledBorder("Availability"));
		JLabel editAvailLabel	= new JLabel("Choose Semester");
		JPanel editAvailOptionPane = new JPanel(new GridLayout(4,1));
		final JCheckBox fallAvailCheck = new JCheckBox("Fall");
		final JCheckBox sprgAvailCheck = new JCheckBox("Spring");
		final JCheckBox wntrAvailCheck = new JCheckBox("Winter");
		final JCheckBox summAvailCheck = new JCheckBox("Summer");
		editAvailOptionPane.add(fallAvailCheck);
		editAvailOptionPane.add(sprgAvailCheck);
		editAvailOptionPane.add(wntrAvailCheck);
		editAvailOptionPane.add(summAvailCheck);
		editAvailPane.add(editAvailLabel);
		editAvailPane.add(editAvailOptionPane);
		
		
		JButton submitButton = new JButton("Submit");
		submitButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				try{
				// id
				String id = editIDField.getText();
				// title
				String title = editTitleField.getText();
				// credit number
				float creditNum = Float.parseFloat(editCredNumField.getText());
				// prerequisites
				
				
				// availability
//				System.out.println("Fall: "   + fallAvailCheck.isSelected());
//				System.out.println("Spring: " + sprgAvailCheck.isSelected());
//				System.out.println("Winter: " + wntrAvailCheck.isSelected());
//				System.out.println("Summer: " + summAvailCheck.isSelected());
				

				JOptionPane.showMessageDialog(null, "ADD FUNCTIONALITY");
				
				GUI_Function_Library.undisplayFrame(frame);
				
				} catch (Exception e2)
				{
					GUI_Function_Library.exceptionHandler("EDIT COURSE: SUBMIT:" , e2);
				};
			}
		});
		
		JPanel mainPane = new JPanel();
		mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.PAGE_AXIS));
		
		mainPane.add(editIDPane);
		mainPane.add(editTitlePane);
		mainPane.add(editCredNumPane);
		mainPane.add(editPrereqPane);
		mainPane.add(editAvailPane);
		mainPane.add(submitButton);
		
		return mainPane;
	}

	@Override
	public void passBack(String cmd, String message) throws Exception {
		if(cmd.equals("add_prereqs"))
		{
			prereqList = new LinkedList<JCheckBox>();
			editPrereqChosenPane = new JPanel();
			editPrereqChosenPane.setLayout(new BoxLayout(editPrereqChosenPane, BoxLayout.PAGE_AXIS));
			String[] courses = message.split(",");
			for(String courseID: courses)
			{
				if(!(courseID == null) && !courseID.isEmpty())
				{
					JCheckBox courseCheckBox = new JCheckBox(courseID);
					editPrereqChosenPane.add(courseCheckBox);
					prereqList.add(courseCheckBox);
//					System.out.println("Prereckled: " + courseID);
				}
			}
//			editPrereqChosenPane.validate();
			frame.validate();
			JOptionPane.showMessageDialog(null, "So far, still doesn't display nicely");
		}
		
	}
}
